<template>
  <div>
    <p class="title">课程管理</p>
    <div style="padding: 10px 0">
      <el-select style="margin: 0 10px;width: 150px;" v-model="searchValue" placeholder="请查询筛选条件">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <el-input style="margin-right:10px; width: 300px" placeholder="请输入关键字进行查询" suffix-icon="el-icon-search"
        v-model="searchContent"></el-input>
      <el-button style="width: 80px;" type="primary" @click="load">查询</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary" @click="handeradd">新增课程 <i class="el-icon-circle-plus-outline"></i></el-button>
      <!-- 有接口 -->
      <el-button type="danger" @click="delBatch">批量删除 <i class="el-icon-delete"></i></el-button>
      <a href="http://localhost:8088/user/export" target="_blank">
        <el-button type="warning" class="ml-5">导出 <i class="el-icon-top"></i></el-button>
      </a>

      <!-- <el-dropdown split-button type="success" class="ml-5">
        按学生学号顺序显示
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-model="ustatuss" @click.native="handbyNomal">正序</el-dropdown-item>
          <el-dropdown-item v-model="ustatuss" @click.native="handbyunNomal">逆序</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown> -->
    </div>
    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
      :header-cell-style="{ 'text-align': 'center' }" :cell-style="{ 'text-align': 'center' }"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection">
      </el-table-column>
      <el-table-column prop="cid" label="课程编号">
      </el-table-column>
      <el-table-column prop="term" label="修读学期">
      </el-table-column>
      <el-table-column prop="name" label="课程名">
      </el-table-column>
      <el-table-column prop="teacher" label="授课教师">
      </el-table-column>
      <el-table-column prop="period" label="总学时">
      </el-table-column>
      <el-table-column prop="credit" label="学分">
      </el-table-column>
      <el-table-column prop="location" label="地点">
      </el-table-column>
      <el-table-column prop="week" label="周次">
      </el-table-column>
      <el-table-column prop="section" label="节次">
      </el-table-column>
      <el-table-column prop="attr" label="课程属性">
      </el-table-column>
      <el-table-column prop="capacity" label="课容量">
      </el-table-column>
      <el-table-column prop="selectNum" label="已选人数">
      </el-table-column>
      <el-table-column label="处理操作" width="150">
        <template slot-scope="scope">
          <el-button @click="showDetail(scope.$index)" size="small">详情</el-button>
          <el-button size="small" type="primary" @click="handerEdit(scope.row)">编辑</el-button>
        </template>
        <!-- <el-table-column label="处理操作">
        <template slot-scope="scope">
          <el-popconfirm class="ml-5" confirm-button-text='确定' cancel-button-text='我再想想' icon="el-icon-info"
            icon-color="red" title="你确定删除吗？" @confirm="del(scope.row.cid)">
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm> -->
        <!-- </template> -->
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <el-dialog title="课程信息" :visible.sync="dialogFormVisible" width="30%">
        <el-form label-width="100px" size="small">
          <el-form-item label="课程编号" prop="cid">
            <el-input placeholder="例如：C2222102910" v-model="form.cid" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="课程名">
            <el-input placeholder="例如：计算机组成原理" v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="总学时" placeholder="只能填写数字" prop="period" :rules="[
            // { required: true, message: '年龄不能为空' },
            { type: 'number', message: '学时必须为数字值' }]">
            <el-input v-model.number="form.period" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="课程学分" placeholder="只能填写数字" prop="credit" :rules="[
            // { required: true, message: '年龄不能为空' },
            { type: 'number', message: '学分必须为数字值' }]">
            <el-input v-model.number="form.credit" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="课程地点">
            <el-input placeholder="例如：浮山校区 敏思楼301" v-model="form.location" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="课程周次">
            <el-input placeholder="例如：1-8（周）" v-model="form.week" autocomplete="off"></el-input>
          </el-form-item> 
          <!-- 课程周次-->
          <!-- <div>
            <p>课程周次</p>
          </div>
          <template>
          <el-checkbox-group style="margin-left: 50px;" v-model="checkboxGroup3" size="small">
            <el-checkbox-button v-for="city in cities" :label="city" :key="city">{{ city }}</el-checkbox-button>
          </el-checkbox-group></template> -->
          <el-form-item label="课程节次">
            <el-input placeholder="例如：星期四第7、8节" v-model="form.section" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="课程类型">
            <el-select v-model="attr" placeholder="请选择" style='width:100%;' @change="selectedValueBygender"
              :formatter="stateFormatGender" clearable="true" filterable>
              <el-option value="必修">
                必修
              </el-option>
              <el-option value="选修">
                选修
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="课容量" placeholder="只能填写数字" prop="capacity" :rules="[
            { type: 'number', message: '学时必须为数字值' }]">
            <el-input v-model.number="form.capacity" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="已选人数" v-if="flag == 1" prop="capacity">
            <el-button type="danger" plain>清空所有选课人数</el-button>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <el-dialog title="课程详情" :visible.sync="dialogFormVisible1" width="50%">
      <el-descriptions style="margin: 20px 10px;" :column="3" border>
        <el-descriptions-item label="课程编号">{{ tableData[chooseIndex].cid }}</el-descriptions-item>
        <el-descriptions-item label="修读学期">{{ tableData[chooseIndex].term }}</el-descriptions-item>
        <el-descriptions-item label="课程名">{{ tableData[chooseIndex].name }}</el-descriptions-item>
        <el-descriptions-item label="授课教师">{{ tableData[chooseIndex].teacher }}</el-descriptions-item>
        <el-descriptions-item label="总学时">{{ tableData[chooseIndex].period }}</el-descriptions-item>
        <el-descriptions-item label="学分">{{ tableData[chooseIndex].credit }}</el-descriptions-item>
        <el-descriptions-item label="地点">{{ tableData[chooseIndex].location }}</el-descriptions-item>
        <el-descriptions-item label="周次">{{ tableData[chooseIndex].week }}</el-descriptions-item>
        <el-descriptions-item label="节次">{{ tableData[chooseIndex].section }}</el-descriptions-item>
        <el-descriptions-item label="课程属性">{{ tableData[chooseIndex].attr }}</el-descriptions-item>
        <el-descriptions-item label="课容量">{{ tableData[chooseIndex].capacity }}</el-descriptions-item>
        <el-descriptions-item label="选课人数">{{ tableData[chooseIndex].selectNum }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>
<script>
  const cityOptions = ['周一', '周二', '周三', '周四','周五','周六','周天'];
export default {
  name: "comment",
  data() {
    return {
      chooseIndex: 0,
      flag: 0,
      checkboxGroup3: ['周一'],
      cities: cityOptions,
      options: [{
        value: '选项1',
        label: '课程编号'
      }, {
        value: '选项2',
        label: '课程名'
      }, {
        value: '选项3',
        label: '授课教师'
      }, {
        value: '选项4',
        label: '地点'
      }],
      rules: {
        cid: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
      },
      searchValue: "",
      tableData: [{
        cid: 'C2222102910',
        term: '2020秋',
        name: '计算机组成原理',
        teacher: '毛爱英',
        period: '32',
        credit: '2.5',
        location: '浮山校区 博知楼250',
        week: '1-8',
        section: '47',
        attr: '必修',
        capacity: '32',
        selectNum: '12',
      },

      {
        cid: 'C2222102910',
        term: '2020秋',
        name: '计算机组成原理',
        teacher: '毛爱英',
        period: '32',
        credit: '2.5',
        location: '浮山校区 博知楼250',
        week: '1-8',
        section: '47',
        attr: '必修',
        capacity: '32',
        selectNum: '12',
      }],
      period: "",
      credit: "",
      attr: "",
      capacity: "",
      form: {
        cid: 'C2222102910',
        term: '2020秋',
        name: '计算机组成原理',
        teacher: '毛爱英',
        period: '32',
        credit: '2.5',
        location: '浮山校区 博知楼250',
        week: '1-8',
        section: '47',
        attr: '必修',
        capacity: '32',
        selectNum: '12',
      },
      total: 0,
      pageNum: 1,
      pageSize: 5,
      uname: "",
      ptitle: "",
      searchContent: "",
      form: {},
      dialogFormVisible1: false,
      dialogFormVisible: false,
      multipleSelection: [],
    }
  },
  created() {
    this.load();
  },
  methods: {
    handeradd() {
      this.dialogFormVisible = true
      this.flag = 0
      this.form = {}
    },
    handerEdit(row) {
      this.form = row
      this.flag = 1
      this.attr = this.form.attr
      this.dialogFormVisible = true
    },
    load() {
      this.request.get("/comment/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          uname: this.uname,
          ptitle: this.ptitle,
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.records
        this.total = res.total
      })

    },
    del(id) {
      this.request.delete("/comment/delete/" + id).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    // 显示课程具体信息
    showDetail(index) {
      this.chooseIndex = index;
      this.dialogFormVisible1 = true;
    },
    delBatch() {
      let cids = this.multipleSelection.map(v => v.cid)
      this.request.post("/comment/del/batch", cids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // console.log(val)
    },
    selectedValueBygender() {
      if (this.attr == "必修") {
        this.form.attr = '必修'
      }
      else
        this.form.attr = '选修'
      console.log(1111);
      console.log(this.form.attr);
    }, handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
  }
}
</script>
<style>
.headerBg {
  background-color: #eee !important;

}

.title {
  font-size: 18px;
  color: #303133;
  margin: 10px;
}
</style>